package com.funtester.seven.day5;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.funtester.seven.day4.HttpUtil;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;

//import static com.funtester.seven.day4.HttpUtil.*;

public class Activity extends HttpUtil {

    public static String HOST = "http://123.56.99.53:9000";

    public static String Login = "/event/api/admin/";

    public static String User_Login = "/event/api/login/";

    public static String Register = "/event/api/register/";

    public static String AddActivity = "/event/api/add_event/";

    public static String ActivytiList = "/event/api/get_eventlist/";

    public static String ActivytiInfo = "/event/api/get_eventdetail/";

    public static String Order = "/event/api/order/";

    public static String uid;

    public static String token;

    /**
     * 是否第一次请求
     */
    private static boolean key = true;

    /**
     * 作业,创建10个用户,创建一个活动(人数限制8个),然后用10个用户去报名这个活动,统计8个成功的订单ID,2个失败的用户ID
     */

    public static void main(String[] args) {
//        login();
//        addActivity("fun", 200, 10);
//        JSONObject ac1 = addActivity("北京活动哈1哈", 20, 50);
//        register("funt362", "FunT21es121r@88.com", "13120448219");
//        register("funt2342", "FunT32es121r@88.com", "13181448219");
        JSONObject alist = activityList(1);
//        activityInfo(866);
//        order(867);
//        alist.getString("event_list");
//        Object a = alist.get("event_list");
//        JSONArray b = changeObjectToJsonArray(a);

        JSONArray event_list = alist.getJSONArray("event_list");
        for (int i = 0; i < event_list.size(); i++) {
            JSONObject act = event_list.getJSONObject(i);
            output(act);
//            Integer id = act.getInteger("id");
//            JSONObject order = order(id);
//            String order_code = order.getString("order_code");
//            System.out.println("订单号:" + order_code);
        }
    }


    /**
     * 预定
     *
     * @param eid
     * @return
     */
    public static JSONObject order(int eid) {
        String url = HOST + Order;
        JSONObject params = new JSONObject();
        params.put("eid", eid);
        JSONObject response = postRes(url, params);
        output(response);
        return response;
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public static JSONObject activityInfo(int id) {
        String url = HOST + ActivytiInfo;
        JSONObject params = new JSONObject();
        params.put("id", id);
        JSONObject response = getRes(url, params);
        output(response);
        return response;
    }

    /**
     * 查询活动列表
     *
     * @param status
     * @return
     */
    public static JSONObject activityList(int status) {
        String url = HOST + ActivytiList;
        JSONObject params = new JSONObject();
        params.put("status", status);
        JSONObject response = getRes(url, params);
        output(response);
        return response;
    }

    /**
     * 注册新用户
     *
     * @param name
     * @param email
     * @param phone
     * @return
     */
    public static JSONObject register(String name, String email, String phone) {
        String url = HOST + Register;
        JSONObject params = new JSONObject();
        params.put("username", name);
        params.put("password", "123456789");
        params.put("phone", phone);
        params.put("email", email);
        JSONObject response = postRes(url, params);
        output(response);
        return response;
    }

    /**
     * 添加活动接口
     *
     * @param title
     * @param limit
     * @param price
     * @return
     */
    public static JSONObject addActivity(String title, int limit, int price) {
        String url = HOST + AddActivity;
        JSONObject params = new JSONObject();
        params.put("title", title);
        params.put("limit", limit);
        params.put("address", "北京");
        params.put("time", "2021-12-21 16:00:00");
        params.put("price", price);
        params.put("type", "户外");
        JSONObject response = postRes(url, params);
        output(response);
        return response;
    }

    /**
     * 登录
     *
     * @return
     */
    public static JSONObject login() {
        String url = HOST + Login;
        JSONObject params = new JSONObject();
        params.put("username", "admin");
        params.put("password", base64Encode("123admin"));
        JSONObject response = postRes(url, params);
//        output(response);
        token = response.getString("token");
        uid = response.getString("uid");
        return response;
    }

    /**
     * 普通用户登录
     *
     * @param username
     * @param password
     * @return
     */
    public static JSONObject loginUser(String username, String password) {
        String url = HOST + User_Login;
        JSONObject params = new JSONObject();
        params.put("username", username);
        params.put("password", base64Encode("123" + password));
        JSONObject response = postRes(url, params);
//        output(response);
        token = response.getString("token");
        uid = response.getString("uid");
        return response;
    }

    /**
     * 获取post响应
     *
     * @return
     */
    public static JSONObject postRes(String url, JSONObject params) {
        HttpPost post = getPost(url, params);//表单
//        HttpPost post = getPost(url, params.toString());//JSON
        JSONObject response = res(post);
        return response;
    }

    /**
     * 获取GET响应
     *
     * @param url
     * @param params
     * @return
     */
    public static JSONObject getRes(String url, JSONObject params) {
        HttpGet get = getGet(url, params);
        JSONObject response = res(get);
        return response;
    }

    /**
     * 根据请求获取响应
     *
     * @param requst
     */
    public static JSONObject res(HttpRequestBase requst) {
        boolean contains = requst.getURI().toString().contains(Login);
        if (!contains && uid == null) {
            login();
        }
        requst.addHeader("uid", uid);
        requst.addHeader("key", token);
        //非第一次请求,sleep 3s
        if (key) {
            key = false;
        } else {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        JSONObject response = getResponse(requst);
        return response;
    }


}
